import { call, put, takeLatest } from 'redux-saga/effects';
import { NEW_USER,addUser } from '../actions/users.action';
import Message from '../components/SimpleLayer/Message';
import API from '../services';

function showErr(content){
	Message.error({
		top:'6px',
		width:'300',
		content,
		delayAutoClose:2000,
		yes:false
	});
}


function* AddUserSaga() {
	yield takeLatest(NEW_USER, function* (action) {
	   	try {
	      	const res = yield call(API.AddUser, action.data);
	  		if (res.success) {
	  			yield put(addUser(action.data));
	  			Message.info({
		          delayAutoClose:1200,
		          top:'6px',
		          width:300,
		          content:res.result
		        });
			}else{
				showErr(res.result);
			}
	   } catch (e) {
	      	showErr(e.message);
	   }
	});
}

export default AddUserSaga;